/*
 * 系统项目名称：综合决策分析系统-version2.0
 * 文件名：ShopCompanyAction.java
 * 描述：
 * 版权：Copyright 2015-2020 义乌市跨境电商供应链管理有限公司-版权所有
 * 修改人：yangjx
 * 修改时间：2016-3-7 下午3:16:05
 * 版本号：V1.1
 */
package com.ywt.cda.action;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ywt.cda.access.Access;
import com.ywt.cda.action.model.ShopCompanyModel;
import com.ywt.cda.domain.Ana2ndData;
import com.ywt.cda.domain.AnaImportData;
import com.ywt.cda.domain.UserAgent;
import com.ywt.cda.domain.enums.OrderByEnum;
import com.ywt.cda.enums.FunctionsEnum;
import com.ywt.cda.service.ParamValidateService;
import com.ywt.cda.service.ShopCompanyService;
import com.ywt.cda.utils.DateUtils;

/**
 * （一句话功能简述） <br>
 * （功能详细描述）
 * 
 * @author yangjx
 * @version V1.0
 * @see [相关类/方法]
 * @since 2016-3-7 下午3:16:05
 */
@Controller
public class ShopCompanyAction {

	private Logger logger = Logger.getLogger(ShopCompanyAction.class);

	@Resource
	private ParamValidateService paramValidateService;
	@Resource
	private ShopCompanyService shopCompanyService;

	/**
	 * 出口-电商分析 getExportShopCompanyAnalyze(这里用一句话描述这个方法的作用) <br>
	 * (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param shopCompanyModel
	 * @param model
	 * @return
	 * @exception
	 * @since 2016-3-11 下午1:57:55
	 * @author yangjx
	 */
	@RequestMapping("export/shops")
	@Access(FunctionsEnum.EXPORT_SHOPS)
	public String getExportShopCompanyAnalyze(
			@ModelAttribute("shopCompanyModel") ShopCompanyModel shopCompanyModel,
			Model model, UserAgent userAgent) {
		if (null == shopCompanyModel) {

			// 默认查近七天
			shopCompanyModel = new ShopCompanyModel();
			Date date = new Date();
			String today = DateUtils.format(date, "yyyy-MM-dd");
			shopCompanyModel.setStartTime(DateUtils.getSpecifiedDayBefore(
					today, 6));
			shopCompanyModel.setEndTime(today);
			model.addAttribute("default", true);
		}
		if (null != shopCompanyModel) {
			if (StringUtils.isBlank(shopCompanyModel.getStartTime())
					&& StringUtils.isBlank(shopCompanyModel.getEndTime())) {
				// 默认查近七天
				
				Date date = new Date();
				String today = DateUtils.format(date, "yyyy-MM-dd");
				shopCompanyModel.setStartTime(DateUtils.getSpecifiedDayBefore(
						today, 6));
				shopCompanyModel.setEndTime(today);
				model.addAttribute("default", true);
			}
		}

		// 第一步：校验输入参数
		if (!paramValidateService.validate(shopCompanyModel)) {
			logger.info("error param!");
			model.addAttribute("error", "error param!");
			return "error";
		}

		Ana2ndData ana2ndData = new Ana2ndData();
		ana2ndData.setMainName("中国国际图书贸易集团有限公司");
		ana2ndData.setSumNum_(new BigDecimal(0));
		ana2ndData.setSumNum(new BigDecimal(0));
		ana2ndData.setAvgPrice(new BigDecimal(0));
		ana2ndData.setSumPkg(new BigDecimal(0));
		ana2ndData.setSumPkg(new BigDecimal(0));
		ana2ndData.setSumPrice(new BigDecimal(0));
		ana2ndData.setSumPrice_(new BigDecimal(0));
		ana2ndData.setMainCodeCount(new BigDecimal(0));
		// 第二步：进行数据库查询
		// 票数排名
		List<Ana2ndData> datas = shopCompanyService.getTopNShopData(
				shopCompanyModel, 10, OrderByEnum.SUM_NUM);
		if(datas.size() == 0){
			datas.add(ana2ndData);
		}
		for(Ana2ndData data: datas){
			if(data.getMainCode() != null && data.getMainCode().equals("1108919052")){
				data.setMainCode("中国国际图书贸易集团有限公司");
			}
			if(data.getCode() != null && data.getCode().equals("1108919052")){
				data.setMainCode("中国国际图书贸易集团有限公司");
			}
		}
//		datas.get(0).setMainCode("中国国际图书贸易集团有限公司");
		// 金额排名
		List<Ana2ndData> datas1 = shopCompanyService.getTopNShopData(
				shopCompanyModel, 10, OrderByEnum.SUM_PRICE);
		if(datas1.size() == 0){
			datas1.add(ana2ndData);
		}
		for(Ana2ndData data: datas1){
			if(data.getMainCode() != null && data.getMainCode().equals("1108919052")){
				data.setMainCode("中国国际图书贸易集团有限公司");
			}
			if(data.getCode() != null && data.getCode().equals("1108919052")){
				data.setMainCode("中国国际图书贸易集团有限公司");
			}
		}
//		datas1.get(0).setMainCode("中国国际图书贸易集团有限公司");
		// 件数排名
		List<Ana2ndData> datas2 = shopCompanyService.getTopNShopData(
				shopCompanyModel, 10, OrderByEnum.SUM_PKG);
		if(datas2.size() == 0){
			datas2.add(ana2ndData);
		}
		for(Ana2ndData data: datas2){
			if(data.getMainCode() != null && data.getMainCode().equals("1108919052")){
				data.setMainCode("中国国际图书贸易集团有限公司");
			}
			if(data.getCode() != null && data.getCode().equals("1108919052")){
				data.setMainCode("中国国际图书贸易集团有限公司");
			}
		}
//		datas2.get(0).setMainCode("中国国际图书贸易集团有限公司");
		// SKU
		List<Ana2ndData> datas4 = shopCompanyService.getTopNShopSku(
				shopCompanyModel, 10);
		if(datas4.size() == 0){
			datas4.add(ana2ndData);
		}
		for(Ana2ndData data: datas4){
			if(data.getMainCode() != null && data.getMainCode().equals("1108919052")){
				data.setMainCode("中国国际图书贸易集团有限公司");
			}
			if(data.getCode() != null && data.getCode().equals("1108919052")){
				data.setMainCode("中国国际图书贸易集团有限公司");
			}
		}
//		datas4.get(0).setMainCode("中国国际图书贸易集团有限公司");
		// 客单价排名
		List<Ana2ndData> datas3 = shopCompanyService.getTopNShopData(
				shopCompanyModel, 10, OrderByEnum.AVG_PRICE);
		if(datas3.size() == 0){
			datas3.add(ana2ndData);
		}
		for(Ana2ndData data: datas3){
			if(data.getMainCode() != null && data.getMainCode().equals("1108919052")){
				data.setMainCode("中国国际图书贸易集团有限公司");
			}
			if(data.getCode() != null && data.getCode().equals("1108919052")){
				data.setMainCode("中国国际图书贸易集团有限公司");
			}
		}
//		datas3.get(0).setMainCode("中国国际图书贸易集团有限公司");

		// 第三步：组装数据
		JSONArray result = new JSONArray();
		JSONArray result1 = new JSONArray();
		JSONArray result2 = new JSONArray();
		JSONObject result3 = new JSONObject();
		JSONArray result4 = new JSONArray();
		shopCompanyService.toJsonArray(datas, result, OrderByEnum.SUM_NUM);
		shopCompanyService.toJsonArray(datas1, result1, OrderByEnum.SUM_PRICE);
		shopCompanyService.toJsonArray(datas2, result2, OrderByEnum.SUM_PKG);
		
		shopCompanyService.toJsonObject(datas3, result3, OrderByEnum.AVG_PRICE);

		shopCompanyService.toJsonArraySku(datas4, result4);
		logger.info(result.toJSONString());
		logger.info(result4.toJSONString());
		model.addAttribute("numarray", result.toJSONString());
		model.addAttribute("pricearray", result1.toJSONString());
		model.addAttribute("pkgarray", result2.toJSONString());
		model.addAttribute("avgobj", result3.toJSONString());
		model.addAttribute("skuarray", result4.toJSONString());
		
		model.addAttribute("yes", shopCompanyModel.getSelectButton());
		return "export/shopcompany";
	}
	
	@RequestMapping("import/shops")
	@Access(FunctionsEnum.IMPORT_SHOPS)
	public String getImportShopCompanyAnalyze(
			@ModelAttribute("shopCompanyModel") ShopCompanyModel shopCompanyModel,
			Model model, UserAgent userAgent) {
		if (null == shopCompanyModel) {

			// 默认查近七天
			shopCompanyModel = new ShopCompanyModel();
			Date date = new Date();
			String today = DateUtils.format(date, "yyyy-MM-dd");
			shopCompanyModel.setStartTime(DateUtils.getSpecifiedDayBefore(
					today, 6));
			shopCompanyModel.setEndTime(today);
			model.addAttribute("default", true);
		}
		if (null != shopCompanyModel) {
			if (StringUtils.isBlank(shopCompanyModel.getStartTime())
					&& StringUtils.isBlank(shopCompanyModel.getEndTime())) {
				// 默认查近七天
				
				Date date = new Date();
				String today = DateUtils.format(date, "yyyy-MM-dd");
				shopCompanyModel.setStartTime(DateUtils.getSpecifiedDayBefore(
						today, 6));
				shopCompanyModel.setEndTime(today);
				model.addAttribute("default", true);
			}
		}

		// 第一步：校验输入参数
		if (!paramValidateService.validate(shopCompanyModel)) {
			logger.info("error param!");
			model.addAttribute("error", "error param!");
			return "error";
		}

		// 第二步：进行数据库查询
		// 票数排名
		List<AnaImportData> datas = shopCompanyService.getTopNShopImportData(
				shopCompanyModel, 10, OrderByEnum.SUM_NUM);
		// 金额排名
		List<AnaImportData> datas1 = shopCompanyService.getTopNShopImportData(
				shopCompanyModel, 10, OrderByEnum.SUM_PRICE);
		// 件数排名
		List<AnaImportData> datas2 = shopCompanyService.getTopNShopImportData(
				shopCompanyModel, 10, OrderByEnum.SUM_PKG);
		// SKU
		List<Ana2ndData> datas4 = shopCompanyService.getTopNShopImportSku(
				shopCompanyModel, 10);
		// 客单价排名
		List<AnaImportData> datas3 = shopCompanyService.getTopNShopImportData(
				shopCompanyModel, 10, OrderByEnum.AVG_PRICE);

		// 第三步：组装数据
		JSONArray result = new JSONArray();
		JSONArray result1 = new JSONArray();
		JSONArray result2 = new JSONArray();
		JSONObject result3 = new JSONObject();
		JSONArray result4 = new JSONArray();
		shopCompanyService.toJsonArrayImport(datas, result, OrderByEnum.SUM_NUM);
		shopCompanyService.toJsonArrayImport(datas1, result1, OrderByEnum.SUM_PRICE);
		shopCompanyService.toJsonArrayImport(datas2, result2, OrderByEnum.SUM_PKG);
		
		shopCompanyService.toJsonObjectImport(datas3, result3, OrderByEnum.AVG_PRICE);

		shopCompanyService.toJsonArraySku(datas4, result4);
		logger.info(result.toJSONString());
		logger.info(result4.toJSONString());
		model.addAttribute("numarray", result.toJSONString());
		model.addAttribute("pricearray", result1.toJSONString());
		model.addAttribute("pkgarray", result2.toJSONString());
		model.addAttribute("avgobj", result3.toJSONString());
		model.addAttribute("skuarray", result4.toJSONString());
		
		model.addAttribute("yes", shopCompanyModel.getSelectButton());
		return "import/shopcompany";
	}
	
}
